package leetCode;

public class t59 {
    public static void main(String[] args) {
        Solution.generateMatrix(3);
    }
   static class Solution {
        public static int[][] generateMatrix(int n) {
            int ret[][]=new int[n][n];
            int top=0;
            int bottom=n;
            int left=0;
            int right=n;
            int num=1;
            while(left<right){
                for (int i=left;i<right;i++){
                    ret[top][i]=num;
                    num++;
                }
                top++;
                for (int i=top;i<bottom;i++){
                    ret[i][right-1]=num;
                    num++;
                }
                right--;
                for (int i=right-1;i>=left;i--){
                    ret[bottom-1][i]=num;
                    num++;
                }
                bottom--;
                for (int i=bottom-1;i>=top;i--){
                    ret[i][left]=num;
                    num++;
                }
                left++;
            }
            return ret;
        }
    }
}
